import Vue from 'vue'
import Router from 'vue-router'
import Layout from '@/layout'

Vue.use(Router)
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true,
    meta: { title: '登录' }
  },
  {
    path: '/auth-redirect',
    component: () => import('@/views/login/auth-redirect'),
    hidden: true
  },
  {
    path: '/404',
    component: () => import('@/views/error-page/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error-page/401'),
    hidden: true
  },
  {
    path: '/profile',
    component: Layout,
    redirect: '/profile/index',
    hidden: true,
    children: [
      {
        path: 'index',
        component: () => import('@/views/profile/index'),
        name: 'Profile',
        meta: { title: 'Profile', icon: 'user', noCache: true }
      }
    ]
  }
]

export const asyncRoutes = [
  {
    path: '/',
    component: Layout,
    name: 'Index',
    redirect: '/index',
    meta: {
      title: '区域管理',
      icon: 'list',
      roles: ['admin']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/index'),
        name: 'index',
        meta: { title: '区域管理', noCache: true }
      },
      {
        path: 'extension',
        component: () => import('@/views/index/components/extension-content'),
        name: 'extension',
        meta: { title: '推广信息', noCache: true },
        hidden: true
      }
    ]
  },

  {
    path: '/user',
    component: Layout,
    name: 'User',
    redirect: '/user',
    meta: {
      title: '用户管理',
      icon: 'user',
      roles: ['admin']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/user'),
        name: 'user',
        meta: { title: '用户管理', noCache: true }
      },
      {
        path: 'vip',
        component: () => import('@/views/user/vip'),
        name: 'vip',
        meta: { title: '会员等级', noCache: true }
      }
    ]
  },

  {
    path: '/category',
    component: Layout,
    name: 'Category',
    redirect: '/category',
    meta: {
      title: '分类管理',
      icon: 'component',
      roles: ['admin']
    },
    children: [
      {
        path: 'index',
        component: () => import('@/views/category'),
        name: 'category',
        meta: { title: '分类管理', noCache: true }
      }
    ]
  },

  {
    path: '/product',
    component: Layout,
    redirect: '/product',
    name: 'Product',
    meta: { title: '产品管理', icon: 'component', roles: ['admin'] },
    children: [
      {
        path: 'index',
        component: () => import('@/views/product'),
        name: 'product',
        meta: { title: '产品管理', noCache: true }
      },
      {
        path: 'section',
        component: () => import('@/views/product/section'),
        name: 'section',
        meta: { title: '栏目管理', noCache: true }
      }
    ]
  },

  {
    path: '/config',
    component: Layout,
    redirect: '/config/userAgreement',
    name: 'config',
    meta: { title: '配置管理', icon: 'el-icon-s-operation', roles: ['admin'] },
    children: [
      {
        path: 'userAgreement',
        component: () => import('@/views/config/user-agreement'),
        name: 'userAgreement',
        meta: { title: '用户协议及隐私政策', noCache: true }
      },
      {
        path: 'deliveryExplain',
        component: () => import('@/views/config/delivery-explain'),
        name: 'deliveryExplain',
        meta: { title: '配送说明', noCache: true }
      },
      {
        path: 'qa',
        component: () => import('@/views/qa'),
        name: 'qa',
        meta: { title: '帮助中心问答', noCache: true }
      },
      {
        path: 'other',
        component: () => import('@/views/config/other-config'),
        name: 'other',
        meta: { title: '其他配置', noCache: true }
      }
    ]
  },

  {
    path: '/order',
    component: Layout,
    redirect: '/order/index',
    name: 'order',
    meta: { title: '订单管理', icon: 'el-icon-s-order', roles: ['admin'] },
    children: [
      {
        path: 'index',
        component: () => import('@/views/order'),
        name: 'orderManager',
        meta: { title: '订单管理', noCache: true }
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
